/*
 * 
 */

package org.opu.gui.docviewer;

import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.FontTable;
import org.apache.poi.hwpf.model.StyleDescription;
import org.apache.poi.hwpf.model.StyleSheet;
import org.apache.poi.hwpf.usermodel.CharacterProperties;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Table;

/**
 *
 * @author yura
 */
public class DocumentParser extends GUIBuilder{

    private StyleSheet styleSheet;
    private FontTable fontTable;
    private StyleContext styleContext;

    /**
     *  Document help parser
     *
     * @param styleSheet
     * @param FontTable
     * @param styleDocument 
     */
    public DocumentParser(HWPFDocument word) {
        styleSheet = word.getStyleSheet();
        fontTable = word.getFontTable();
        styleContext = DocUTIL.getStyleSheet(word);
    }


    /**
     * Get the value of styleSheet
     *
     * @return the value of styleSheet
     */
    public StyleSheet getStyleSheet() {
        return styleSheet;
    }

    /**
     * Set the value of styleSheet
     *
     * @param styleSheet new value of styleSheet
     */
    public void setStyleSheet(StyleSheet styleSheet) {
        this.styleSheet = styleSheet;
    }

    /**
     * Get the value of string
     *
     * @return the value of string
     */
    public FontTable getFontTable() {
        return fontTable;
    }

    /**
     * Set the value of string
     *
     * @param string new value of string
     */
    public void setFontTable(FontTable string) {
        this.fontTable = string;
    }

    /**
     *  Get style name of current paragraph
     *
     * @param paragraph paragraph were from getted style
     * @return name of style
     */
    public String getStyleName(Paragraph paragraph) {
        short id = paragraph.getStyleIndex();
        StyleDescription paragraphStyle = styleSheet.getStyleDescription(id);
        return paragraphStyle.getName();
    }

    @Override
    public int appendTable(Table table) {
        int i = super.appendTable(table);
        insertString("\n", null);
        return i;
    }

    @Override
    public void append(Paragraph paragraph) {
        int numChar = paragraph.numCharacterRuns();
        for (int i = 0; i < numChar; i++) {
            CharacterRun chr = paragraph.getCharacterRun(i);
            insertString(chr.text(), getAttributeSet(paragraph, chr));
        }
        insertString("\n", styleContext.getStyle(getStyleName(paragraph)));
    }

    @Override
    public MutableAttributeSet getAttributeSet(Paragraph paragraph, CharacterRun tb) {
        short sindex = paragraph.getStyleIndex();
        //CharacterProperties chrStl = styleSheet.getCharacterStyle(sindex);
        Style parAtr = styleContext.getStyle(getStyleName(paragraph));
        MutableAttributeSet style = new SimpleAttributeSet(parAtr);

        short s = tb.getSubSuperScriptIndex();
        int fz = DocUTIL.ptFontSize(tb.getFontSize()) / 2 * 3;
        
        StyleConstants.setBold(style, tb.isBold());
        StyleConstants.setFontSize(style, fz);
        StyleConstants.setForeground(style, DocUTIL.decodColor(tb.getColor()));
        StyleConstants.setItalic(style, tb.isItalic());
        StyleConstants.setStrikeThrough(style, tb.isStrikeThrough());
        StyleConstants.setSubscript(style, s > 0);
        StyleConstants.setSuperscript(style, s < 0);
        StyleConstants.setUnderline(style, tb.getUnderlineCode() > 0);

        StyleConstants.setAlignment(style, paragraph.getFontAlignment());
        StyleConstants.setFirstLineIndent(style, paragraph.getFirstLineIndent()/10);
        StyleConstants.setFontFamily(style, tb.getFontName());

        return style;
    }

}
